<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <meta name="description" content="Partytown Test Page" />
    <title>fetch() / XMLHttpRequest</title>
    <style>
      body {
        font-family: -apple-system, BlinkMacSystemFont, Segoe UI, Helvetica, Arial, sans-serif,
          Apple Color Emoji, Segoe UI Emoji;
        font-size: 12px;
      }
      h1 {
        margin: 0 0 15px 0;
      }
      ul {
        list-style-type: none;
        margin: 0;
        padding: 0;
      }
      a {
        display: block;
        padding: 16px 8px;
      }
      a:link,
      a:visited {
        text-decoration: none;
        color: blue;
      }
      a:hover {
        background-color: #eee;
      }
      li {
        display: flex;
        margin: 15px 0;
      }
      li strong,
      li code,
      li button {
        white-space: nowrap;
        flex: 1;
        margin: 0 5px;
      }
    </style>
    <script>
      partytown = {
        logCalls: true,
        logGetters: true,
        logSetters: true,
        logStackTraces: false,
        logScriptExecution: true,
        logMainAccess: true,
      };
    </script>
    <script src="/~partytown/debug/partytown.js"></script>
  </head>
  <body>
    <h1>fetch() / XMLHttpRequest</h1>
    <ul>
      <li>
        <strong>window.fetch(url) text</strong>
        <code>
          <span id="testFetchText"></span>
        </code>
        <script type="text/partytown">
          (async function () {
            const elm = document.getElementById('testFetchText');
            const rsp = await window.fetch('text.txt');
            const text = await rsp.text();
            elm.textContent = text;
            elm.className = 'testFetchText';
          })();
        </script>
      </li>

      <li>
        <strong>fetch(URL) json</strong>
        <code>
          <span id="testFetchJson"></span>
        </code>
        <script type="text/partytown">
          (async function () {
            const url = new URL('data.json', location);
            const elm = document.getElementById('testFetchJson');
            const rsp = await fetch(url);
            const data = await rsp.json();
            elm.textContent = JSON.stringify(data);
            elm.className = 'testFetchJson';
          })();
        </script>
      </li>

      <li>
        <strong>fetch Set-Cookie</strong>
        <code>
          <span id="testFetchCookie"></span>
        </code>
        <script type="text/partytown">
          (async function () {
            const url = new URL('/api/cookie?name=server-test-fetch', location.origin);
            const elm = document.getElementById('testFetchCookie');
            const rsp = await fetch(url, {
              credentials: 'include',
            });
            elm.textContent = document.cookie;
            elm.className = 'testFetchCookie';
          })();
        </script>
      </li>

      <li>
        <strong>XMLHttpRequest</strong>
        <code id="testXMLHttpRequest"></code>
        <script type="text/partytown">
          (function () {
            const elm = document.getElementById('testXMLHttpRequest');

            const xhr = new XMLHttpRequest();
            xhr.addEventListener('load', function () {
              elm.textContent = this.responseText;
              elm.className = 'testXMLHttpRequest';
            });
            xhr.open('GET', 'text.txt');
            xhr.send();
          })();
        </script>
      </li>

      <li>
        <strong>XMLHttpRequest native</strong>
        <code id="testXMLHttpRequestNative"></code>
        <script type="text/partytown">
          (function () {
            const elm = document.getElementById('testXMLHttpRequestNative');

            const test1 = /\s*function\s+XMLHttpRequest\(\)\s*{\s*\[native code\]\s*}\s*/m;

            const invalidXMLHttpRequest = function () {
              if (
                XMLHttpRequest &&
                XMLHttpRequest.prototype
              ) {
                const t = XMLHttpRequest.prototype.toString();
                return !(test1.test(t));
              }
              return true;
            };

            elm.textContent = !invalidXMLHttpRequest();
          })();
        </script>
      </li>

      <li>
        <strong>XMLHttpRequest constructor native</strong>
        <code id="testXMLHttpRequestCstrNative"></code>
        <script type="text/partytown">
          (function () {
            const elm = document.getElementById('testXMLHttpRequestCstrNative');

            const test1 = /\s*function\s+XMLHttpRequest\(\)\s*{\s*\[native code\]\s*}\s*/m;
            const test2 = /\[object XMLHttpRequestConstructor\]/m;

            const invalidXMLHttpRequest = function () {
              if (
                XMLHttpRequest &&
                XMLHttpRequest.prototype &&
                XMLHttpRequest.prototype.constructor
              ) {
                const t = XMLHttpRequest.prototype.constructor.toString();
                return !(test1.test(t) || test2.test(t));
              }
              return true;
            };

            elm.textContent = !invalidXMLHttpRequest();
          })();
        </script>
      </li>

      <li>
        <strong>XMLHttpRequest Set-Cookie</strong>
        <code>
          <span id="testXMLHttpRequestCookie"></span>
        </code>
        <script type="text/partytown">
          (async function () {
            const url = new URL('/api/cookie?name=server-test-xhr', location.origin);
            const elm = document.getElementById('testXMLHttpRequestCookie');
            const xhr = new XMLHttpRequest();
            xhr.addEventListener('load', function () {
              elm.textContent = document.cookie;
              elm.className = 'testXMLHttpRequestCookie';
            });
            xhr.open('GET', url);
            xhr.withCredentials = true;
            xhr.send();
          })();
        </script>
      </li>

      <script type="text/partytown">
        (function () {
          document.body.classList.add('completed');
        })();
      </script>
    </ul>

    <hr />
    <p><a href="/tests/">All Tests</a></p>
  </body>
</html>
